package top.kaoshanji.p1ch03a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import top.kaoshanji.u1constant.FileConstant;

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * 代码清单 p1ch03a-13：内存映射文件的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/7 17:03
 */
public class FileChannelExample05 {

    private static final Logger logger = LogManager.getLogger(FileChannelExample05.class);

    public static void main(String[] args) {
        logger.info("...代码清单 p1ch03a-13：内存映射文件的使用示例...");

        try (FileChannel channel = FileChannel.open(Paths.get(FileConstant.SRC_COPY_FILE),
                StandardOpenOption.READ, StandardOpenOption.WRITE)) {
            MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size());

            byte b = buffer.get(1024 * 1024);
            buffer.put(5 * 1024 * 1024, b);
            buffer.force();


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
